package com.lsp.test;

import lombok.extern.slf4j.Slf4j;

import java.util.Queue;
@Slf4j
public class Producer implements Runnable {
    private Queue<String> bags;
    private int maxSize;

    public Producer(Queue<String> bags, int maxSize) {
        this.bags = bags;
        this.maxSize = maxSize;
    }

    @Override
    public void run() {
        int i = 0;
        while (true){
            i++;
            synchronized (bags){
                if(bags.size() == maxSize){
                    log.info("bags满了！");
                    try {
                        bags.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
                }
                log.info("生产者生产：bag"+ i);
                bags.add("bag"+i);
                bags.notify();
            }
        }

    }
}
